<template>
  <div class="coupon_index">
    <!-- 全选，取消全选 -->
    <div class="checkAll" v-show="couponList.length">
      <p><span>优惠券核销</span><small>(可一次性勾选多张核销)</small></p>
      <van-checkbox v-model="isCheckAll" shape="square">
        全选
      </van-checkbox>
    </div>

    <!-- 优惠券列表展示 -->
    <div class="center">
      <van-checkbox-group v-model="currentSelectList" ref="checkboxGroup">
        <div
          class="coupon"
          :class="[
            ele.couponTemplateInfo.couponType == 1 ? 'direct' : '',
            ele.couponTemplateInfo.couponType == 2 ? 'rate' : '',
            ele.couponTemplateInfo.couponType == 3 ? 'exchange' : '',
            ele.couponTemplateInfo.couponType == 4 ? 'carwash' : ''
          ]"
          v-for="ele in couponList"
          :key="ele.couponUserId"
        >
          <div class="left">
            <div
              class="amount"
              v-if="
                ele.couponTemplateInfo.couponType == 1 ||
                  ele.couponTemplateInfo.couponType == 4
              "
            >
              ￥
              <span class="value">{{ ele.couponTemplateInfo.couponAmt }}</span>
            </div>
            <div class="amount" v-if="ele.couponTemplateInfo.couponType == 2">
              <span class="value">{{ ele.couponTemplateInfo.couponAmt }}</span>
              %
            </div>
            <div class="condition">
              {{
                ele.couponIssueInfo.couponThresholdAmt == 0
                  ? "无门槛"
                  : "满" + ele.couponIssueInfo.couponThresholdAmt + "元可用"
              }}
            </div>
            <div class="exchangeName">
              {{ ele.couponTemplateInfo.couponName }}
            </div>
          </div>
          <div class="right">
            <div class="title">
              <span class="type"
                >{{ ele.couponTemplateInfo.couponType == 1 ? "立减劵" : ""
                }}{{ ele.couponTemplateInfo.couponType == 2 ? "折扣劵" : ""
                }}{{ ele.couponTemplateInfo.couponType == 3 ? "兑换劵" : "" }}
                {{ ele.couponTemplateInfo.couponType == 4 ? "洗车劵" : "" }}
              </span>
              <span class="state">
                {{ ele.canUse == "1" ? "可使用" : "" }}
                {{ !ele.canUse == "1" ? "暂时不可用" : "" }}
              </span>
            </div>
            <div
              class="content"
              :class="[ele.couponTemplateInfo.couponType != 3 ? 'notip' : '']"
              style="position: relative;"
            >
              <div class="items">
                <div
                  class="item valid"
                  v-if="ele.couponTemplateInfo.couponType != 3"
                >
                  油品：<span>{{ ele.oils }}</span>
                </div>
                <div class="item scope">
                  适用：
                  <span class="hidden-scroll">
                    {{
                      ele.couponTemplateInfo.couponType == 1 ||
                      ele.couponTemplateInfo.couponType == 2 ||
                      ele.couponTemplateInfo.couponType == 4
                        ? ele.couponStaNames
                        : ""
                    }}{{
                      ele.couponTemplateInfo.couponType == 3
                        ? ele.verStaNames
                        : ""
                    }}
                  </span>
                </div>
                <div class="item valid">
                  截止：<span>{{ ele.couponEffectiveTime }}</span>
                </div>
                <div class="item from">
                  来源：<span>{{ ele.couponIssueInfo.issueName }}</span>
                </div>
              </div>
            </div>
            <!-- 复选框 -->
            <div class="btnCheckbox">
              <van-checkbox :name="ele.couponNo" shape="square" />
            </div>
            <!-- 备注提示 -->
            <div class="tip" v-if="ele.couponTemplateInfo.couponType == 3">
              {{ ele.couponTemplateInfo.couponDetails }}
            </div>
          </div>
        </div>
      </van-checkbox-group>
    </div>
    <!-- 空列表 -->
    <div
      v-if="couponList.length == 0"
      style="
        text-align: center;
        font-weight: bold;
        margin-top: 2rem;
        margin-bottom: 3rem;
      "
    >
      暂未符合条件的优惠劵
    </div>

    <van-pagination
      v-show="total > 0"
      v-model="currentpage"
      :total-items="total"
      :items-per-page="queryForm.pageSize"
    />

    <div class="sbuBtn" v-show="currentSelectList.length > 0">
      <van-button
        type="primary"
        @click="handleSubmit"
        :disabled="currentSelectList.length <= 0"
        :loading="loading"
        >确认使用</van-button
      >
    </div>
  </div>
</template>

<script>
import { mapMutations, mapGetters } from "vuex";
import Vue from "vue";
import { Pagination, Icon, Checkbox, CheckboxGroup, Button,Toast  } from "vant";
Vue.use(Icon);
Vue.use(Pagination);
Vue.use(Checkbox);
Vue.use(CheckboxGroup);
Vue.use(Button);
Vue.use(Toast);

export default {
  // head() {
  //   return {
  //     title: "自助核销"
  //   };
  // },
  props: {
    couponList: {
      type: Array,
      default: () => []
    },
    queryForm: {
      type: Object,
      default: () => {}
    },
    total: {
      type: Number,
      default: 0
    },
    couponTypeOfUrl:{ 
      type:String,
      default:()=>{}
    },
  },
  data() {
    return {
      currentSelectList: [], // 选中列表
      currentpage: this.queryForm.pageNum,
      loading: false
    };
  },
  computed: {
    ...mapGetters({
      userInfo: "authen/userInfo"
    }),
    // 是否全选
    isCheckAll: {
      get() {
        return (
          this.currentSelectList.length &&this.currentSelectList.length === this.couponList.length
        );
      },
      set(val) {
        if (val) {
          this.$refs.checkboxGroup.toggleAll(true); // 勾选了全选
        } else {
          this.$refs.checkboxGroup.toggleAll(false); // 取消勾选
        }
      }
    }
  },
  watch: {
    currentpage(val) {
      let data = { ...this.queryForm };
      data.pageNum = val;
      this.$emit("update:queryForm", data);
      this.$emit("reloadCouponList");
      this.isCheckAll = false;
    }
  },
  methods: {
    // 更新列表数据
    getCouponList() {
      this.$emit("reloadCouponList");
    },

    // 确认核销
    async handleSubmit() {
      this.loading = true;
      const params = {
        couponNoList: this.currentSelectList,
        stationId: this.queryForm.stationId
      };
      const res = await this.$axios.$post("/couponDestroyList", params);
      this.loading = false;
      // debugger;

      if (res.retCode === 0) {
        // window.location.href = `/${this.enterpriseId}/coupon/autoHexiao_success`;
        this.$router.push({
          path: `/${this.enterpriseId}/coupon/autoHexiao_success`,
          query: { dataInfo: JSON.stringify(res.data),couponType:this.couponTypeOfUrl,stationId: this.queryForm.stationId }
        });
      }else{ 
        this.$toast.fail(res&&res.message)
      }
      if (res.retCode === 500 && res.message === "油站id不能为空") {
        alert("请重新扫码进入");
      }
    }
  }
};
</script>

<style lang="scss" scoped>
.loading {
  width: 100px;
  height: 100px;
  position: fixed;
  top: 50%;
  left: 50%;
  display: flex;
  justify-content: center;
  align-items: center;
  background: rgba(0, 0, 0, 0.6);
  transform: translate(-50%, -50%);
}
.btnCheckbox {
  position: absolute;
  right: 0.2rem;
  top: 0.1rem;
  width: 0.5rem;
}
.coupon_index {
  width: 7.5rem;
  background: #ffffff;
  padding: 0 0.3rem 0 0.3rem;
  box-sizing: border-box;
  min-height: 100vh;
  margin-top: 0.3rem;

  > .top {
    width: 6.9rem;
    height: 1rem;

    .fixed {
      position: fixed;
      top: 0;
      left: 0;
      width: 7.5rem;
      height: 1rem;
      padding: 0 0.3rem 0 0.3rem;
      box-sizing: border-box;
      background: #ffffff;
      display: flex;
      justify-content: space-around;
      align-items: center;
      span {
        height: 0.45rem;
        font-size: 0.32rem;
        font-weight: 400;
        color: #aaaaaa;
        line-height: 0.45rem;
        letter-spacing: 0.01rem;
      }
      span.selected {
        color: #111111;
      }
    }
  }

  // 全选
  .checkAll {
    display: flex;
    align-items: center;
    justify-content: space-between;
    small {
      color: #666;
    }
  }
  > .center {
    .coupon {
      display: flex;
      justify-content: space-between;
      height: 2.8rem;
      margin-bottom: 0.3rem;
      box-shadow: 0rem 0rem 0.1rem 0.04rem rgba(233, 223, 223, 0.5);
      > .left {
        display: flex;
        flex-direction: column;
        justify-content: center;
        text-align: center;
        width: 2.2rem;
        height: 2.8rem;
        text-align: center;

        .amount {
          display: block;
          font-size: 0.4rem;
          font-weight: 500;
          color: #ffffff;
          line-height: 0.56rem;
          text-align: center;
          margin-top: 0rem;
          .value {
            font-size: 0.6rem;
          }
        }
        .condition {
          font-size: 0.24rem;
          font-weight: 400;
          color: #ffffff;
          line-height: 0.33rem;
          text-align: center;
          margin-top: 0.25rem;
        }
        .exchangeName {
          display: none;
          font-size: 0.35rem;
          font-weight: 500;
          color: #ffffff;
          line-height: 0.56rem;
        }
      }
      > .right {
        width: 4.56rem;
        height: 2.45rem;
        display: flex;
        flex-direction: column;
        justify-content: space-between;
        // margin: 0.08rem 0 0.05rem 0;
        position: relative;
        padding: 0.2rem 0rem;
        box-sizing: content-box;
        .title {
          height: 0.3rem;
          font-size: 0.24rem;
          font-weight: 600;
          line-height: 0.33rem;
          .type {
            width: 1.13rem;
            height: 0.3rem;
            line-height: 0.3rem;
            display: inline-block;
            background: #f8c;
            border-radius: 0.19rem;
            font-size: 0.24rem;
            font-weight: 600;
            color: #ffffff;
            text-align: center;
          }
          .state {
            margin-left: 0.2rem;
            color: #7f7f7f;

            height: 0.33rem;
            font-size: 0.24rem;
            font-weight: 600;
            line-height: 0.33rem;
          }
        }
        .content {
          display: flex;
          justify-content: space-between;
          margin: 0.05rem 0 0.05rem 0;
          height: 1.4rem;

          .items {
            width: 4.3rem;
            height: 100%;
            display: flex;
            justify-content: space-around;
            flex-direction: column;
            .item {
              color: #272626;
              font-size: 0.21rem;
              > span {
                width: 3.3rem;
                display: inline-block;
                color: #272626;
                font-size: 0.22rem;
                font-weight: bold;
                overflow: scroll; /* 超出部分隐藏 */
                white-space: nowrap; /* 文本不换行 */
              }
            }
            .from {
              display: block;
            }
          }
          > .btn {
            display: flex;
            flex-direction: column;
            justify-content: center;
            margin-right: 0.1rem;
            > button {
              height: 0.6rem;
              background: linear-gradient(143deg, #fad965 0%, #f3b235 100%);
              border-radius: 0.2rem;
              border: none;
              outline: none;
              color: #ffffff;
              line-height: 0.33rem;
              font-size: 0.3rem;
              font-weight: 400;
              overflow: hidden; /* 超出部分隐藏 */
              white-space: nowrap; /* 文本不换行 */
              display: none;
            }
          }
        }
        .notip {
          height: 1.8rem;
        }
        .tip {
          margin-right: 0.2rem;
          border-top: 0.02rem dashed #d8d8d8;
          height: 0.49rem;
          font-size: 0.22rem;
          font-weight: 400;
          color: #7f7f7f;
          line-height: 0.23rem;
          padding-top: 0.12rem;
          box-sizing: border-box;
          overflow: scroll; /* 超出部分隐藏 */
          white-space: nowrap;
          /*  text-overflow: ellipsis;*/
        }
      }
    }

    .carwash {
      > .left {
        background: url("../../../static/coupon/quan5.2x.png") no-repeat;
        background-size: 100% 100%;
      }

      > .right {
        > .title > .type {
          background: #19c79b;
        }

        > .content > .btn button {
          background: linear-gradient(143deg, #21d9b4 0%, #0fb582 100%);
          display: block;
        }
      }
    }

    .direct {
      > .left {
        background: url("../../../static/coupon/quan1.2x.png") no-repeat;
        background-size: 100% 100%;
      }
      > .right {
        > .title > .type {
          background: #f8ce57;
        }
        > .content > .btn button {
          background: linear-gradient(143deg, #fad965 0%, #f3b235 100%);
        }
      }
    }
    .rate {
      > .left {
        background: url("../../../static/coupon/quan2.2x.png") no-repeat;
        background-size: 100% 100%;
      }
      > .right {
        > .title > .type {
          background: #eb8573;
        }
        > .content > .btn button {
          background: linear-gradient(143deg, #f4a796 0%, #e67462 100%);
        }
      }
    }
    .exchange {
      > .left {
        background: url("../../../static/coupon/quan3.2x.png") no-repeat;
        background-size: 100% 100%;
        > .amount,
        > .condition {
          display: none;
        }
        > .exchangeName {
          display: inline-block;
          width: 1.5rem;
          margin: 0 auto;
        }
      }
      > .right {
        > .title > .type {
          background: #19c79b;
        }
        > .content {
          > .items {
            width: 3.3rem;
            .item span {
              width: 2.3rem;
            }
          }
          > .btn button {
            background: linear-gradient(143deg, #21d9b4 0%, #0fb582 100%);
            display: block;
          }
        }
      }
    }
    .expired {
      > .left {
        background: url("../../../static/coupon/quan4.2x.png") no-repeat;
        background-size: 100% 100%;
      }
      > .right {
        > .title > .type {
          background: #bfbfbf;
        }
        > .content > .btn button {
          display: none;
        }
      }
    }
  }
  > .bottom {
    margin-top: 0.8rem;
    margin-bottom: 0.3rem;
    text-align: center;
    font-size: 0.22rem;
    font-family: PingFangSC-Regular, PingFang SC;
    font-weight: 400;
    color: rgba(0, 0, 0, 0.25);
    line-height: 0.32rem;
    letter-spacing: 1px;
  }

  .sbuBtn {
    background-color: #ffffff;
    position: fixed;
    bottom: 0px;
    width: 100%;
    padding: 10px 0;
    left: 0;
    text-align: center;
    button {
      width: 80%;
    }
  }
}
.hidden-scroll::-webkit-scrollbar {
  display: none;
}
</style>
